#include "ssicloud_vs_maintain.h"

using namespace ssicloud;
VSMaintain::VSMaintain()
    : ItemObject<MaintainNode>(LQ_BASE_URL "/vs/maintain/getByVin"
                           , OBJECT_NAME_VSMaintain
                           , NetUrl::NET_HTTP_METHOD_GET)
{
    //mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);

    mUrl.append("vin", VIN_STRING);
}

bool VSMaintain::requestAll()
{
    return obtain();
}

int VSMaintain::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    // GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, vin, mItem.vin);
        JSON_VALUETRING_GET_DOUBLE(dataJson, additionalMile, mItem.additionalMile);
        JSON_VALUETRING_GET_DOUBLE(dataJson, remainMile, mItem.remainMile);
        JSON_VALUETRING_GET_DOUBLE(dataJson, currentMile, mItem.currentMile);
        JSON_VALUETRING_GET_DOUBLE(dataJson, maintainMile, mItem.maintainMile);

        // profile();
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

void VSMaintain::profile()
{
    GEN_Printf(LOG_INFO, "---------维保服务 获取里程信息-------");
    GEN_Printf(LOG_INFO, "vin: %s", mItem.vin.c_str());
    GEN_Printf(LOG_INFO, "设置里程: %d", mItem.additionalMile);
    GEN_Printf(LOG_INFO, "距离下次保养里程: %0.02g", mItem.remainMile);
    GEN_Printf(LOG_INFO, "当前里程: %0.02g", mItem.currentMile);
    GEN_Printf(LOG_INFO, "总行使里程: %0.02g", mItem.maintainMile);
    GEN_Printf(LOG_INFO, "---------完成-------");
}

VSMaintainSet::VSMaintainSet()
    : ItemObject<VSMaintainSetNode>(LQ_BASE_URL "/vs/maintainPlan/set"
                           , OBJECT_NAME_VSMaintainSet
                           , NetUrl::NET_HTTP_METHOD_POST)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
}

bool VSMaintainSet::requestSetMile(int mile)
{
    snprintf(m_body, sizeof(m_body), "{\"vin\": \"%s\","
                                     "\"intervalChassisMile\": %d,"
                                     "\"intervalEngineMile\": %d"
                                     "}", VIN_STRING, mile, mile);
    mUrl.appendBody(m_body);
    return obtain();
}

int VSMaintainSet::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *code = cJSON_GetObjectItem(root, "code");
    if (code && *(code->valuestring) == '1')
    {
        // cJSON_GetStringValue()
        // GEN_Printf(LOG_DEBUG, "code value = %d, %s", (int)code->valuedouble);
        GEN_Printf(LOG_INFO, "upload info successfully.");
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

void VSMaintainSet::profile()
{

}


VSMaintainGetRemind::VSMaintainGetRemind()
    : ItemObject<VSMaintainGetRemindNode>(LQ_BASE_URL "/vs/maintainRecord/getRemind"
                           , OBJECT_NAME_VSMaintainGetRemind
                           , NetUrl::NET_HTTP_METHOD_GET)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
    mUrl.append("vin", VIN_STRING);
}

bool VSMaintainGetRemind::requestAll()
{
    return obtain();
}

int VSMaintainGetRemind::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, nextChassisMile, mItem.nextChassisMile);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, nextEngineMile, mItem.nextEngineMile);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, currentMile, mItem.currentMile);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, remainChassisMile, mItem.remainChassisMile);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, remainEngineMile, mItem.remainEngineMile);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, lastChassisMile, mItem.lastChassisMile);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, lastEngineMile, mItem.lastEngineMile);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, lastChassisDate, mItem.lastChassisDate);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, lastEngineDate, mItem.lastEngineDate);
        profile();
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

void VSMaintainGetRemind::profile()
{
    GEN_Printf(LOG_INFO, "---------保养-获取保养提醒-------");
    GEN_Printf(LOG_INFO, "nextChassisMile: %s", mItem.nextChassisMile.c_str());
    GEN_Printf(LOG_INFO, "nextEngineMile: %s", mItem.nextEngineMile.c_str());
    GEN_Printf(LOG_INFO, "currentMile: %s", mItem.currentMile.c_str());
    GEN_Printf(LOG_INFO, "remainChassisMile: %s", mItem.remainChassisMile.c_str());
    GEN_Printf(LOG_INFO, "remainEngineMile: %s", mItem.remainEngineMile.c_str());
    GEN_Printf(LOG_INFO, "lastChassisMile: %s", mItem.lastChassisMile.c_str());
    GEN_Printf(LOG_INFO, "lastEngineMile: %s", mItem.lastEngineMile.c_str());
    GEN_Printf(LOG_INFO, "lastChassisDate: %s", mItem.lastChassisDate.c_str());
    GEN_Printf(LOG_INFO, "lastEngineDate: %s", mItem.lastEngineDate.c_str());

    GEN_Printf(LOG_INFO, "---------完成-------");
}

VSMaintainRecordAdd::VSMaintainRecordAdd()
    : ItemObject<VSMaintainSetNode>(LQ_BASE_URL "/vs/maintainRecord/add"
                           , OBJECT_NAME_VSMaintainRecordAdd
                           , NetUrl::NET_HTTP_METHOD_POST)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
}
bool VSMaintainRecordAdd::requestAll(const char *maintainDate, double actualMile, int itemType)
{
    snprintf(m_body, sizeof(m_body), "{\"vin\": \"%s\","
                                     "\"maintainDate\": \"%s\","
                                     "\"actualMile\": %g,"
                                     "\"itemType\": %d"
                                     "}", VIN_STRING, maintainDate, actualMile, itemType);
    mUrl.appendBody(m_body);
    return obtain();
}

int VSMaintainRecordAdd::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *code = cJSON_GetObjectItem(root, "code");
    if (code && *(code->valuestring) == '1')
    {
        // cJSON_GetStringValue()
        // GEN_Printf(LOG_DEBUG, "code value = %d, %s", (int)code->valuedouble);
        GEN_Printf(LOG_INFO, "upload info successfully.");
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

void VSMaintainRecordAdd::profile()
{

}

ssicloud::VSMaintainRecordPage::VSMaintainRecordPage()
    : ListObject<VSMaintainRecordPageLNode>(LQ_BASE_URL "/vs/maintainRecord/selectPage"
                           , OBJECT_NAME_VSMaintainRecordPage
                           , NetUrl::NET_HTTP_METHOD_GET)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
    mUrl.append("vin", VIN_STRING);
}

bool ssicloud::VSMaintainRecordPage::requestAll(const char *startDate, const char *endDate, int pageNum, int pageSize)
{
    if (isLoading()) return true;

    m_startDate = startDate;
    m_endDate   = endDate;
    m_pageNum   = SFString::number(pageNum);
    m_pageSize  = SFString::number(pageSize);

    mUrl.appendChange("startDate", m_startDate.c_str());
    mUrl.appendChange("endDate", m_endDate.c_str());
    mUrl.appendChange("pageNum", m_pageNum.c_str());
    mUrl.appendChange("pageSize", m_pageSize.c_str());

    return obtain();
}

void ssicloud::VSMaintainRecordPage::profile()
{
    ListTable<VSMaintainRecordPageLNode>::iterator it = mNodes.begin();
    GEN_Printf(LOG_INFO, "---------保养-分页查询保养登记-------");

    for (; it != mNodes.end(); ++it)
    {
         GEN_Printf(LOG_DUMP, "保养日期:%s, 保养里程:%s,  保养项:%s, 录入时间:%s",
                    it->maintainDate.string(), it->actualMile.c_str(),
                    it->itemType.c_str(), it->createTime.c_str());
    }
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int ssicloud::VSMaintainRecordPage::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        cJSON *records = cJSON_GetObjectItem(dataJson, "records");
        if (records)
        {
            for (cJSON *item = records->child; NULL != item; item = item->next )
            {
                VSMaintainRecordPageLNode tmp;

                JSON_VALUETRING_SWAP_BYTESTRING(item, maintainDate, tmp.maintainDate);
                JSON_VALUETRING_SWAP_BYTESTRING(item, actualMile, tmp.actualMile);
                JSON_VALUETRING_SWAP_BYTESTRING(item, itemType, tmp.itemType);
                JSON_VALUETRING_SWAP_BYTESTRING(item, createTime, tmp.createTime);

                // GEN_Printf(LOG_DEBUG, "items size=%d", tmp.items.size());
                mNodes.push_back(tmp);
            }
        }

        if (mNodes.empty())
        {
            GEN_Printf(LOG_WARN, "load Category is empty.");
            ret = KL_DATA_PRISER_EMPTY;
        } else
        {
            profile();
        }
    } else
    {
        cJSON *code = cJSON_GetObjectItem(root, "code");
        if (code && *(code->valuestring) == '1')
        {
            GEN_Printf(LOG_INFO, "get empty result successfully.");
        } else {

            GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
            ret = KL_DATA_PRISER_JSOC_ERROR;
        }
    }

    cJSON_Delete(root);
    return ret;
}
